﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using WindowsFormsApp1;

/// <summary>
/// 另起一个线程跑Loading SplashScreen窗口，由主进程执行耗时操作
/// </summary>
public class SplashScreenManager
{
    //自定义传入窗口，异步显示
    private FrmLoading LoadingForm;

    /// <summary>
    /// 初始化SplashScreenManager，需要传入一个Form窗体对象
    /// </summary>
    /// <param name="LoadingForm">LoadingForm To Show</param>
    public SplashScreenManager(Form frm)
    {
        this.LoadingForm = new FrmLoading(frm);
    }

    private void ShowWaitForm()
    {
        LoadingForm.BringToFront();//放在前端显示
        LoadingForm.Activate(); //当前窗体是LoadingForm
        LoadingForm.ShowDialog();
    }

    /// <summary>
    /// 显示加载窗体
    /// </summary>
    /// <param name="msg">显示文字</param>
    public void ShowLoading(string msg = "正在加载中，请稍候")
    {
        MethodInvoker invoker = new MethodInvoker(ShowWaitForm);
        invoker.BeginInvoke(null, null);
        /*Console.WriteLine("等待Loading窗体实例化"); */
        while (!LoadingForm.IsHandleCreated) { }
        // 把显示窗体放到最前面
        LoadingForm.Invoke(new MethodInvoker(() =>
        {
            LoadingForm.SetMsg(msg);
            LoadingForm.BringToFront();//放在前端显示
            LoadingForm.Activate(); //当前窗体是LoadingForm
        }));
    }

    /// <summary>
    /// 关闭loading窗体
    /// </summary>
    public void CloseWaitForm()
    {
        int err_count = 0;
        try
        {
            LoadingForm.Invoke(new MethodInvoker(() =>
            {
                LoadingForm.Close();
            }));
        }
        catch (Exception)
        {
            //防止未初始化,重复去close,直到OK
            bool isOK = false;
            err_count++;
            while (!isOK && err_count < 20)
            {
                try
                {
                    isOK = true;
                    LoadingForm.Invoke(new MethodInvoker(() =>
                    {
                        LoadingForm.Close();
                    }));
                }
                catch (Exception) { isOK = false; err_count++; }
            }
        }
        finally
        {
        }
    }
}